﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using Warehouse.Services.Repository;
using Warehouse.ViewModels.TESinterfaceDTO;
using WCS.Entities.Api;
using WCS.Entities.BulkStorage;
using WCS.Entities.TES;
using WCS.IServices;
using WCS.ViewModels.AGVDTO;

namespace WCS.Services
{
	public class AGVService: IAGVSerive
	{
		protected readonly Repository<AGVchuDTO> agv;
		public AGVService(Repository<AGVchuDTO> agv)
		{
			this.agv = agv;
		}
		//出库位置
		public string position(int isTOrs)
		{
			var handler = new HttpClientHandler();
			handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
			var httpclient = new HttpClient(handler);

			httpclient.DefaultRequestHeaders.Accept.Clear();
			httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

			HttpContent httpContent = JsonContent.Create(isTOrs, typeof(BulkStorage), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
			{
				Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
			});

			HttpResponseMessage repsone = httpclient.GetAsync("http://10.6.3.1:9099/api/TransferPort/GetAll").Result;

			return string.Empty;
		}

		//呼叫AGV
		public string occupyStation(AGVchuDTO model)
		{
			try
			{
                var handler = new HttpClientHandler();
                handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
                var httpclient = new HttpClient(handler);

                httpclient.DefaultRequestHeaders.Accept.Clear();
                httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpContent httpContent = JsonContent.Create(model, typeof(Salver), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
                {
                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
                });

                HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.3.1:9099/api/Job/PortCreateTask", httpContent).Result;
                if (repsone.IsSuccessStatusCode)
                {
                    Task<string> t = repsone.Content.ReadAsStringAsync();
                    if (t != null)
                    {
                        return t.Result;
                    }
                }
                return string.Empty;
            }
			catch (Exception ex)
			{

				return ex.Message;
			}
			
		}
	}
}
